import json
from rest_framework import serializers


class Jsonserializer(serializers.JSONField):
    default_error_messages = {
        'invalid_json': ('无效的json数据格式')
    }

    def to_representation(self, value):
        print("value: ".format(value))
        return json.loads(value)

    def to_internal_value(self, data):
        print("data: {}".format(data))
        try:
            json.dumps(data)
        except (TypeError, ValueError):
            self.fail('invalid_json')
